package com.bby.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.bby.common.exception.BBYException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.util.ObjectUtils;
import sun.security.provider.MD5;

import java.util.Date;
import java.util.UUID;

/**
 * 工具类
 */
public class BBYUtils {

    static ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    public static boolean isEmpty(Object object){
        return ObjectUtils.isEmpty(object);
    }
    public static boolean isEmpty(String str){
        return str==null||str.length()==0;
    }
    public static boolean isAllEmpty(String ...strs){
        if (strs==null){
            return true;
        }
        for (String str : strs) {
            if (str!=null&&str.length()>0){
                return false;
            }
        }
        return true;
    }
    public static boolean hasEmpty(String ...strs){
        if (strs==null){
            return true;
        }
        for (String str : strs) {
            if (str==null||str.length()==0){
                return true;
            }
        }
        return false;
    }

    /**
     * 有一个为空就报错
     */
    public static void checkEmpty(Object ...objects) throws BBYException {
        if (ObjectUtil.hasEmpty(objects)){
            throw new BBYException(StatusCode.INPUT_ERROR,"参数错误: "+ JSON.toJSONString(objects));
        }
    }
    /**
     * 有一个为空就报错
     */
    public static void checkEmpty(String ...strs) throws BBYException {
        if (hasEmpty(strs)){
            throw new BBYException(StatusCode.INPUT_ERROR,"参数错误: "+ JSON.toJSONString(strs));
        }
    }
    public static boolean isAllEmpty(Object ...objects){
        if (objects==null){
            return true;
        }
        for (Object o : objects) {
            if (o!=null&&!ObjectUtils.isEmpty(o)){
                return false;
            }
        }
        return true;
    }
    public static String uuidStr() {
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    /**
     * 统一反序列化方式，有些自断需要特殊解析
     */
    public static <T> T toBean(String json,Class<T> tClass) throws BBYException {
        try {
            return objectMapper.readValue(json,tClass);
        } catch (JsonProcessingException e) {
            throw new BBYException("json数据解析错误！");
        }
    }

    public static void main(String[] args) throws BBYException {
        System.out.println(toBirthdayDate("20asas18-a12-12"));

    }

    public static Object toBirthdayDate(String value) {
        return DateUtil.parse(value,"yyyy-MM-dd");
    }
}
